/**
 * Created by Boyz.X on 2015/9/23.
 */
appModule.controller('contactsCtr', function ($scope, $state, CommonUtils, $http, $stateParams, $rootScope, $ionicModal, CustomersService) {
  var action = eval('(' + $stateParams.action + ')');
  var selectDeps = [];
  //var sharePer = [];
  //var box = [];
  var tmpShareUsers = [];
  //$scope.keyword;
  /*  $rootScope.currentCustomer.shareUser*/
  //var webserver = 'http://192.168.168.245:8080/eos-default/';
  $scope.$on('$ionicView.beforeEnter', function () {
    $scope.currentCustomer = CustomersService.getCurrentCustomer();
    if ($scope.currentCustomer.users) {
      tmpShareUsers = $scope.currentCustomer.users;
    }
  });

  $ionicModal.fromTemplateUrl('my-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function (modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function () {
    CustomersService.queryAllOrg(function (data) {
      $scope.modal.show();
      $scope.show = $scope.data;
      $scope.Dep = data.OrgName;
    });
  };
  $scope.closeModal = function () {
    $scope.groupkeyword = [];
    for (var i = 0; i < selectDeps.length; i++) {
      $scope.groupkeyword.push(selectDeps[i].orgid);
      console.log($scope.groupkeyword);
    }
    $scope.modal.hide();
  };
  //当我们用到模型时，清除它！
  $scope.$on('$destroy', function () {
    $scope.modal.remove();
  });
  // 当隐藏的模型时执行动作
  $scope.$on('modal.hide', function () {
    // 执行动作
  });
  // 当移动模型时执行动作
  $scope.$on('modal.removed', function () {
    // 执行动作
  });
  /**
   * 获取用户列表
   */
  CustomersService.getAllUsers(function (data) {
    if (data.users) {
      $scope.show = data.users;
      $scope.data = data.users;
    }
  });
  /**
   * 更新共享用户
   */
  $scope.updatePerShare = function () {
    if ($scope.currentCustomer.cid) {
      CustomersService.updateShareUsers($scope.currentCustomer.cid, tmpShareUsers, function (data) {
        $scope.currentCustomer.users = tmpShareUsers;
        CommonUtils.goBack();
      });
    } else {
      $scope.currentCustomer.users = tmpShareUsers;
      CommonUtils.goBack();
    }
  };

  $scope.checked = function (empid) {
    if ($scope.currentCustomer.users) {
      for (var i = 0; i < $scope.currentCustomer.users.length; i++) {
        if ($scope.currentCustomer.users[i].userid == empid) {
          return true;
        }
      }
    }
    return false;
  };
  $scope.checkedDep = function (orgid) {
    for (var i = 0; i < selectDeps.length; i++) {
      if (selectDeps[i].orgid == orgid) {
        return true;
      }
    }
    return false;
  };

  $scope.changStatue = function (user, statue) {
    var tmp = {userid: user.userCode, empname: user.empname, cid: $scope.currentCustomer.id};
    if (statue) {
      tmpShareUsers.push(tmp)
    } else {
      var index = -1;
      for (var i = 0, n = tmpShareUsers.length; i < n; i++) {
        if (tmpShareUsers[i].userid == tmp.userid) {
          index = i;
          break;
        }
      }
      if (index > -1) {
        tmpShareUsers.splice(index, 1);
      }
    }
  };
  $scope.selectDep = function (orgid, statue) {
    if (statue) {
      selectDeps.push({"orgid": orgid})
    } else {
      var index = selectDeps.indexOf(orgid);
      if (index > -1) {
        selectDeps.splice(index, 1);
      }
    }
  };

  $scope.selectAllDep = function (statue) {
    selectDeps = [];
    if (statue == true) {
      for (var i = 0; i < $scope.Dep.length; i++) {
        selectDeps.push({"orgid": $scope.Dep[i].orgid});
      }
    }
  };
});


appModule.filter('groupfilter', function () {
  return function (input, param) {
    var output = [];
    if (param) {
      for (var i = 0; i < param.length; i++) {
        for (var j = 0; j < input.length; j++) {
          if (input[j].orgid == param[i]) {
            output.push(input[j]);
          }
        }
      }
      return output;
    } else {
      return input;
    }
  }
});
